package 单元测试

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
)

type Monster struct {
	Name string `json:"name"`
	Age int `json:"age"`
	Skill string `json:"skill"`
}

func (this *Monster) Store() bool {
	data, err := json.Marshal(this)
	if err != nil{
		fmt.Println("序列化错误", err)
		return false
	}
	filePath := "./monster.txt"
	err = ioutil.WriteFile(filePath, data, 0666)
	if err != nil{
		fmt.Println("写入错误", err)
		return false
	}
	//fmt.Println("成功")
	return true
}

func (this *Monster) ReStore() bool {
	filePath := "./monster.txt"
	data, err := ioutil.ReadFile(filePath)
	if err != nil{
		fmt.Println("读取错误",err)
		return false
	}
	err = json.Unmarshal(data, this)
	if err != nil{
		fmt.Println("反序列化错误",err)
		return false
	}
	//fmt.Println("ok")
	return true
}